package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.*;
import com.sunda.spmswms.mapper.ShelfOnTaskDtlMapper;
import com.sunda.spmswms.mapper.ShelfOnTaskMapper;
import com.sunda.spmswms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

import static com.sunda.spmscommon.Constans.*;

/**
 * <p>
 * 上架任务表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-03-19
 */
@Service
public class ShelfOnTaskServiceImpl extends ServiceImpl<ShelfOnTaskMapper, ShelfOnTask> implements IShelfOnTaskService {

    @Autowired
    ShelfOnTaskDtlMapper shelfOnTaskDtlMapper;

    @Autowired
    ShelfOnTaskMapper shelfOnTaskMapper;

    @Autowired
    ISapDeliveryNoteLogService logService;

    @Autowired
    IShelfOnTaskDtlService shelfOnTaskDtlService;

    @Autowired
    ISapPackageListDtlService iSapPackageListDtlService;

    @Autowired
    ISapDeliveryNoteLogService iSapDeliveryNoteLogService;

    @Autowired
    IDocumentType iDocumentType;

    /**
     * 初始化上、下架任务表头，初始化上架任务明细 备件货
     */
    @Override
    public int generateShelfOnTask(String userId, String createDate, String deliveryNoteSap, String taskType, String pdaSn) {

        Map<String, Object> sapDeliveryNote = this.baseMapper.getSapDeliveryNote(deliveryNoteSap);
        if (sapDeliveryNote == null) {
            return -1;
        }

        QueryWrapper<ShelfOnTask> queryWrapper = new QueryWrapper<ShelfOnTask>().eq("DELIVERY_NOTE_SAP", deliveryNoteSap).eq("TASK_TYPE", taskType).eq("TASK_STATUS", "1");
        int resp = 0;
        if (this.baseMapper.selectOne(queryWrapper) != null) {
            /** 存在 */
            return -1;
        } else {
            /** 不存在则新建 */
            ShelfOnTask shelfOnTask = new ShelfOnTask();
            shelfOnTask.setWerks(sapDeliveryNote.get("werks").toString());
            shelfOnTask.setWhsLocationCode(sapDeliveryNote.get("whsLocationCode").toString());
            shelfOnTask.setCreateUserId(userId);
            shelfOnTask.setCreateDate(createDate);
            shelfOnTask.setTaskStatus("1");
            shelfOnTask.setDeliveryNoteSap(deliveryNoteSap);
            shelfOnTask.setTaskType(taskType);
            shelfOnTask.setPdaSN(pdaSn);
            shelfOnTask.setUpdateUsername(userId);
            String uuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            shelfOnTask.setUuid(uuid);
            resp = this.baseMapper.insert(shelfOnTask);
            generateShelfOnTaskDtl(uuid, deliveryNoteSap);
            /** 记录上架任务日志 */
            if (taskType.equals("1")) {
                logService.insertSapDeliveryNoteLogM(uuid, userId, GENERATE_SHELF_ON_TASK);
            } else {
                logService.insertSapDeliveryNoteLogM(uuid, userId, GENERATE_DEDUCT_SHELF_TASK);
            }
            return resp;
        }

    }

    @Override
    public ShelfOnTask generateShelfOnTaskV1(String userId, String createDate, String deliveryNoteSap, String taskType, String pdaSn) {
        Map<String, Object> sapDeliveryNote = this.baseMapper.getSapDeliveryNote(deliveryNoteSap);
        if (sapDeliveryNote == null) {
            return null;
        }

        QueryWrapper<ShelfOnTask> queryWrapper = new QueryWrapper<ShelfOnTask>().eq("DELIVERY_NOTE_SAP", deliveryNoteSap).eq("TASK_TYPE", taskType).eq("TASK_STATUS", "1");
        int resp = 0;
        if (this.baseMapper.selectOne(queryWrapper) != null) {
            /** 存在 */
            return null;
        } else {
            /** 不存在则新建 */
            ShelfOnTask shelfOnTask = new ShelfOnTask();
            shelfOnTask.setWerks(sapDeliveryNote.get("werks").toString());
            shelfOnTask.setWhsLocationCode(sapDeliveryNote.get("whsLocationCode").toString());
            shelfOnTask.setCreateUserId(userId);
            shelfOnTask.setCreateDate(createDate);
            shelfOnTask.setTaskStatus("1");
            shelfOnTask.setDeliveryNoteSap(deliveryNoteSap);
            shelfOnTask.setTaskType(taskType);
            String uuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            shelfOnTask.setUuid(uuid);
            shelfOnTask.setPdaSN(pdaSn);
            shelfOnTask.setUpdateUsername(userId);
            this.baseMapper.insert(shelfOnTask);
            generateShelfOnTaskDtl(uuid, deliveryNoteSap);

            /** 记录上架任务日志 */
            if (taskType.equals("1")) {
                logService.insertSapDeliveryNoteLogM(uuid, userId, GENERATE_SHELF_ON_TASK);
            } else {
                logService.insertSapDeliveryNoteLogM(uuid, userId, GENERATE_DEDUCT_SHELF_TASK);
            }

            return shelfOnTask;
        }
    }

    /**
     * 初始化上架、下架任务表明细
     */
    void generateShelfOnTaskDtl(String uuid, String deliveryNoteSap) {
        shelfOnTaskDtlService.generateShelfOnTaskDtl(uuid, deliveryNoteSap);
    }

    /**
     * 以deliveryNoteSap为主键完成上、下架任务， 只更改任务表头，不更改行项明细
     */
    @Override
    public ShelfOnTask completeShelfOnTask(String userId, String deliveryNoteSap, String pdaSn, String taskType, String currentTaskStatus, String targetTaskStatus) {
        QueryWrapper<ShelfOnTask> queryWrapper = new QueryWrapper<ShelfOnTask>().eq("DELIVERY_NOTE_SAP", deliveryNoteSap).eq("TASK_TYPE", taskType).eq("TASK_STATUS", currentTaskStatus);
        ShelfOnTask shelfOnTask = this.baseMapper.selectOne(queryWrapper);
        if (shelfOnTask != null) {
            /** 存在则更新 */
            shelfOnTask.setUpdateUsername(userId);
            shelfOnTask.setTaskStatus(targetTaskStatus);
            shelfOnTask.setPdaSN(pdaSn);
            int resp = this.baseMapper.update(shelfOnTask, queryWrapper);
            if (resp == 1) {
                /** 记录上架任务完成日志 */
                logService.insertSapDeliveryNoteLogM(shelfOnTask.getUuid(), userId, CLOSE_SHELF_ON_TASK);
                return shelfOnTask;
            }
        }
        return null;
    }

    // 以uuid为主键，完成上、下架任务，只更改任务表头，不更改行项明细
    @Override
    public ShelfOnTask completeShelfOnTaskByUuid(String userId, String uuid, String pdaSn, String currentTaskStatus, String targetTaskStatus) {
        QueryWrapper<ShelfOnTask> queryWrapper = new QueryWrapper<ShelfOnTask>().eq("UUID", uuid).eq("TASK_STATUS", currentTaskStatus);
        ShelfOnTask shelfOnTask = this.baseMapper.selectOne(queryWrapper);
        if (shelfOnTask == null) {
            /** 不存在，返回 */
            return null;
        }
        /** 存在则更新 */
        shelfOnTask.setUpdateUsername(userId);
        shelfOnTask.setTaskStatus(targetTaskStatus);
        shelfOnTask.setPdaSN(pdaSn);
        this.baseMapper.update(shelfOnTask, queryWrapper);
        return shelfOnTask;

    }

    // 根据条件筛选上、下架任务表头信息
    @Override
    public List<Map<String, Object>> getShelfOnTask(String taskId, String createUserId, String werks, String whsLocationCode, String taskStatus, String updateUsername, String deliveryNoteSap, String pdaSN, String taskType) {
        if (StringUtils.isEmpty(whsLocationCode)) {
            return null;
        }
        List<String> taskStatusList = new ArrayList<>();
        taskId = StringUtils.isNotEmpty(taskId) ? taskId : null;
        werks = StringUtils.isNotEmpty(werks) ? werks : null;
        createUserId = StringUtils.isNotEmpty(createUserId) ? createUserId : null;
        List<String> whsLocationCodeList = Arrays.asList(whsLocationCode.split(","));
        updateUsername = StringUtils.isNotEmpty(updateUsername) ? updateUsername : null;
        /** 任务状态：1待上架；2上架中；3已完成；4待下架；5下架中；6出库待下架；7出库下架中； */
        /** taskStatus 为空则 taskStatusList 默认只查询 1,2,4,5,6,7个状态数据 */
        if (StringUtils.isEmpty(taskStatus)) {
            taskStatusList = Arrays.asList("1", "2", "4", "5", "6", "7");
        } else {
            taskStatusList = Arrays.asList(taskStatus.trim().split(","));
        }
        deliveryNoteSap = StringUtils.isNotEmpty(deliveryNoteSap) ? deliveryNoteSap : null;
        pdaSN = StringUtils.isNotEmpty(pdaSN) ? pdaSN : null;
        taskType = StringUtils.isNotEmpty(taskType) ? taskType : null;
        List<Map<String, Object>> list = shelfOnTaskMapper.getShelfOnTask(taskId, createUserId, werks, whsLocationCodeList, taskStatusList, updateUsername, deliveryNoteSap, pdaSN, taskType);
        return list;
    }

    // 根据uuid调取上、下架任务详情
    @Override
    public Map<String, Object> getShelfOnTaskByUuid(String uuid) {
        Map<String, Object> shelfOnTaskObj = new HashMap<>();
        ShelfOnTask shelfOnTask = shelfOnTaskMapper.selectOne(new QueryWrapper<ShelfOnTask>().eq("UUID", uuid));

        // 获取上架任务失败
        if (null == shelfOnTask) {
            return null;
        }
        /** ===================== 20210511 wayne add begin ======================= */
        /** 当任务类型是 - 2出库任务，且状态为 6出库待下架，7出库下架中，返回出库任务详情时包含出库详情中箱码的储位信息 */
        if ("2".equals(shelfOnTask.getTaskType()) && ("6".equals(shelfOnTask.getTaskStatus()) || "7".equals(shelfOnTask.getTaskStatus()))) {
            List<Map<String, Object>> shelfOnTaskDtls = this.baseMapper.getShelfOnTaskDtlByUuid(uuid);
            shelfOnTaskObj.put("shelfOnTask", shelfOnTask);
            shelfOnTaskObj.put("shelfOnTaskDtls", shelfOnTaskDtls);
            return shelfOnTaskObj;
        }
        /** ===================== 20210511 wayne add end ======================= */
        /** ===================== 20210526 wayne add begin ======================= */
        //List<ShelfOnTaskDtl> shelfOnTaskDtls = shelfOnTaskDtlMapper.selectList(new QueryWrapper<ShelfOnTaskDtl>().eq("UUID", uuid));
        List<Map<String, Object>> shelfOnTaskDtls = this.baseMapper.getShelfOnTaskDtlAndStorage(uuid);
        /** ===================== 20210526 wayne add end ======================= */

        shelfOnTaskObj.put("shelfOnTask", shelfOnTask);
        shelfOnTaskObj.put("shelfOnTaskDtls", shelfOnTaskDtls);
        return shelfOnTaskObj;
    }

    // 根据关联任务uuid获取下加任务明细
    @Override
    public ResponseResult getShelfOffWithDtlsByRelatedUUID(String uuid) {
        try {

            String[] taskStatus = {"4", "5", "6", "7"};
            ShelfOnTask shelfOnTask = shelfOnTaskMapper.selectOne(new QueryWrapper<ShelfOnTask>().eq("RELATE_DOC_NUMBER", uuid).eq("TASK_TYPE", "2").in("TASK_STATUS", taskStatus));

            // 获取上架任务失败
            if (null == shelfOnTask) {
                return ResponseResult.error("获取任务失败");
            }
            /** ===================== 20210511 wayne add begin ======================= */
            /** 当任务类型是 - 2出库任务，且状态为 6出库待下架，7出库下架中，返回出库任务详情时包含出库详情中箱码的储位信息 */
            if ("2".equals(shelfOnTask.getTaskType()) && ("6".equals(shelfOnTask.getTaskStatus()) || "7".equals(shelfOnTask.getTaskStatus()))) {
                List<Map<String, Object>> shelfOnTaskDtls = this.baseMapper.getShelfOnTaskDtlByUuid(shelfOnTask.getUuid());
                return ResponseResult.success().add("shelfOffTask", shelfOnTask).add("shelfOffTaskDtls", shelfOnTaskDtls);
            }
            /** ===================== 20210511 wayne add end ======================= */
            List<ShelfOnTaskDtl> shelfOnTaskDtls = shelfOnTaskDtlMapper.selectList(new QueryWrapper<ShelfOnTaskDtl>().eq("UUID", shelfOnTask.getUuid()));

            return ResponseResult.success().add("shelfOffTask", shelfOnTask).add("shelfOffTaskDtls", shelfOnTaskDtls);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("error when get shelf off tasks");
        }
    }

    // 更改上、下架任务状态
    @Override
    public ShelfOnTask updateShelfOnTask(String uuid, String currentTaskStatus, String targetTaskStatus) {
        QueryWrapper queryWrapper = new QueryWrapper<ShelfOnTask>().eq("UUID", uuid).eq("TASK_STATUS", currentTaskStatus);
        ShelfOnTask shelfOnTask = shelfOnTaskMapper.selectOne(queryWrapper);

        // 获取上架任备失败
        if (null == shelfOnTask) {
            return null;
        }

        shelfOnTask.setTaskStatus(targetTaskStatus);
        this.baseMapper.update(shelfOnTask, queryWrapper);
        return shelfOnTask;
    }

    /**
     * 出库任务初始化生成 下架任务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int genShelfOnTask(String userId, String createDate, WhsOut whsOut) {
        try {
            String whsOutUuid = whsOut.getUuid();
            QueryWrapper<ShelfOnTask> queryWrapper = new QueryWrapper<ShelfOnTask>().eq("RELATE_DOC_NUMBER", whsOutUuid).eq("TASK_TYPE", "2").eq("TASK_STATUS", "6");
            /** 如果出库任务关联单据下已经有一个 待下架的下架任务，则禁止再生成一个下架任务，不存在则新建下架任务 */
            if (this.baseMapper.selectOne(queryWrapper) != null) {
                return -1;
            }
            String uuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            ShelfOnTask shelfOnTask = new ShelfOnTask();
            shelfOnTask.setUuid(uuid);
            shelfOnTask.setWerks(whsOut.getWerks());
            shelfOnTask.setCreateUserId(userId);
            shelfOnTask.setWhsLocationCode(whsOut.getWhsLocationCode());
            shelfOnTask.setCreateDate(createDate);
            /** 出库待下架状态 6 */
            shelfOnTask.setTaskStatus("6");
            /** 1上架任务；2下架任务； */
            shelfOnTask.setTaskType("2");
            shelfOnTask.setRelateDocNumber(whsOutUuid);
            int resp = this.baseMapper.insert(shelfOnTask);
            iSapDeliveryNoteLogService.insertSapDeliveryNoteLogM(uuid, userId, GENERATE_DEDUCT_SHELF_TASK);

            /** 获取唯一箱码的出库任务明细行 */
            List<Map<String, Object>> whsOutDtlX = iSapPackageListDtlService.getWhsOutDtlX(whsOutUuid, whsOut.getGoodsType());
            if (whsOutDtlX.size() > 0) {
                for (int i = 0; i < whsOutDtlX.size(); i++) {
                    ShelfOnTaskDtl shelfOnTaskDtl = new ShelfOnTaskDtl();
                    shelfOnTaskDtl.setUuid(uuid);
                    shelfOnTaskDtl.setTaskRowId(String.valueOf((i + 1) * 10));
                    shelfOnTaskDtl.setBoxNote(whsOutDtlX.get(i).get("boxNote").toString());
                    shelfOnTaskDtl.setQuantity(1.00);
                    shelfOnTaskDtlMapper.insert(shelfOnTaskDtl);
                }
            }
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    /**
     * 临时保存下架任务执行过程，只做数据记录，不计算储位库存
     * {
     * "shelfOnTask": {
     * "uuid": "3e74fd02509f4e0b900c6c5433bc19b7",
     * "currentStatus": "6",
     * "targetStatus": "7",
     * "userId": "999947",
     * "pdaSN": "1234567890"
     * },
     * "shelfOnTaskDtls": [{
     * "uuid": "3e74fd02509f4e0b900c6c5433bc19b7",
     * "taskRowId": 10,
     * "boxNote": "TEST-1009-0101",
     * "actualOperateQty": "1"
     * }, {
     * "uuid": "3e74fd02509f4e0b900c6c5433bc19b7",
     * "taskRowId": 20,
     * "boxNote": "TEST-1009-0102",
     * "actualOperateQty": ""
     * }]
     * }
     *
     * @param doc
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int tempSaveShelfOnTaskDtls(String userId, JSONObject doc) {
        if (!doc.containsKey("shelfOnTask") || !doc.containsKey("shelfOnTaskDtls") || doc.getJSONArray("shelfOnTaskDtls").size() == 0) {
            return -1;
        }
        try {
            JSONObject shelfOnTaskDoc = doc.getJSONObject("shelfOnTask");
            String uuid = doc.getJSONObject("shelfOnTask").getString("uuid");
            if (updateShelfOnTask(shelfOnTaskDoc) > 0) {
                shelfOnTaskDtlMapper.delete(new QueryWrapper<ShelfOnTaskDtl>().eq("UUID", uuid));
                JSONArray shelfOnTaskDtls = doc.getJSONArray("shelfOnTaskDtls");
                for (int i = 0; i < shelfOnTaskDtls.size(); i++) {
                    JSONObject shelfOnTaskDtlObj = shelfOnTaskDtls.getJSONObject(i);
                    ShelfOnTaskDtl shelfOnTaskDtl = new ShelfOnTaskDtl();
                    shelfOnTaskDtl.setUuid(shelfOnTaskDtlObj.getString("uuid"));
                    shelfOnTaskDtl.setTaskRowId(shelfOnTaskDtlObj.getString("taskRowId"));
                    shelfOnTaskDtl.setBoxNote(shelfOnTaskDtlObj.getString("boxNote"));
                    shelfOnTaskDtl.setActualOperateQty(StringUtils.isEmpty(shelfOnTaskDtlObj.getString("actualOperateQty"))
                            ? null : Double.parseDouble(shelfOnTaskDtlObj.getString("actualOperateQty")));
                    shelfOnTaskDtl.setTargetStorage(shelfOnTaskDtlObj.getString("targetStorage"));
                    shelfOnTaskDtl.setUnplanned(shelfOnTaskDtlObj.getString("unplanned"));
                    shelfOnTaskDtlMapper.insert(shelfOnTaskDtl);
                }
                logService.insertSapDeliveryNoteLogM(uuid, userId, UPDATE_DEDUCT_SHELF_TASK);
                return 1;
            }
            return -2;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -3;
        }
    }

    int updateShelfOnTask(JSONObject shelfOnTaskDoc) {
        try {
            String uuid = shelfOnTaskDoc.getString("uuid");
            String currentStatus = shelfOnTaskDoc.getString("currentStatus");
            String targetStatus = shelfOnTaskDoc.getString("targetStatus");
            String pdaSN = shelfOnTaskDoc.getString("pdaSN");
            ShelfOnTask shelfOnTask = this.baseMapper.selectOne(new QueryWrapper<ShelfOnTask>().eq("UUID", uuid).eq("TASK_STATUS", currentStatus));
            if (shelfOnTask == null) {
                return -1;
            }
            shelfOnTask.setTaskStatus(targetStatus);
            shelfOnTask.setPdaSN(pdaSN);
            return this.baseMapper.update(shelfOnTask, new QueryWrapper<ShelfOnTask>().eq("UUID", uuid));
        } catch (Exception e) {
            e.printStackTrace();
            return -2;
        }
    }

    @Override
    public ShelfOnTask getShelfOnTaskByTaskUuid(String uuid) {
        if(StringUtils.isNotEmpty(uuid)) {
            return this.baseMapper.selectOne(new QueryWrapper<ShelfOnTask>().eq("UUID", uuid));
        }
        return null;
    }

    @Override
    public int deleteShelfOnTaskDtl(String uuid) {
        return shelfOnTaskDtlMapper.delete(new QueryWrapper<ShelfOnTaskDtl>().eq("UUID", uuid));
    }

    @Override
    public int insertShelfOnTaskDtl(ShelfOnTaskDtl shelfOnTaskDtl) {
        return shelfOnTaskDtlMapper.insert(shelfOnTaskDtl);
    }

    @Override
    public int updateShelfOnTaskStatus(String uuid, String targetStatus) {
        ShelfOnTask shelfOnTask = new ShelfOnTask();
        shelfOnTask.setUuid(uuid);
        shelfOnTask.setTaskStatus(targetStatus);
        return this.baseMapper.update(shelfOnTask, new QueryWrapper<ShelfOnTask>().eq("UUID", uuid));
    }

    @Override
    public int addShelfOnTask(ShelfOnTask shelfOnTask) {
        return this.baseMapper.insert(shelfOnTask);
    }

    @Override
    public int addShelfOnTaskDtl(ShelfOnTaskDtl shelfOnTaskDtl) {
        return shelfOnTaskDtlMapper.insert(shelfOnTaskDtl);
    }

    /**
     * {
     * "werks": "CN01",
     * "whsLocationCode": "1061",
     * "beginDate": "20210512",
     * "endDate": "20210530",
     * "taskStatus": "3",
     * "taskType": "1",
     * "xQuery": {
     * "documentType": "SAPDN",
     * "documentSpmsId": "1180066354"
     * }
     * }
     */
    @Override
    public ResponseResult queryShelfOnTasks(JSONObject query) {
        try {
            String whsLocationCode = query.getString("whsLocationCode");
            String werks = query.getString("werks");
            if (StringUtils.isEmpty(whsLocationCode) || StringUtils.isEmpty(werks)) {
                return ResponseResult.error("whsLocationCode is mandatory required");
            }
            String beginDate = "".equals(query.getString("beginDate")) ? null : query.getString("beginDate");
            String endDate = "".equals(query.getString("endDate")) ? null : query.getString("endDate");
            String taskStatus = "".equals(query.getString("taskStatus")) ? null : query.getString("taskStatus");
            String taskType = "".equals(query.getString("taskType")) ? null : query.getString("taskType");
            JSONObject xQuery = query.containsKey("xQuery") ? query.getJSONObject("xQuery") : null;
            String deliveryNoteSap = null;
            String relateDocNumber = null;
            if (null != xQuery && StringUtils.isNotEmpty(xQuery.getString("documentType")) && StringUtils.isNotEmpty(xQuery.getString("documentSpmsId"))) {
                DocumentType documentQuery = iDocumentType.getUuidByDocType(xQuery.getString("documentType"), xQuery.getString("documentSpmsId"));
                if (null != documentQuery  && StringUtils.isNotEmpty(documentQuery.getDocumentUuid())) {
                    if (documentQuery.getDocumentType().equals("SAPDN")) {
                        deliveryNoteSap = documentQuery.getDocumentSpmsId();
                    } else {
                        relateDocNumber = documentQuery.getDocumentUuid();
                    }
                }else{
                    return ResponseResult.error("The documentID: " +xQuery.getString("documentSpmsId") +"does not have any ShelfOn/Off tasks or not exist" );
                }
            }

            List<String> taskStatusList = new ArrayList<>();
            /** 任务状态：1待上架；2上架中；3已完成；4待下架；5下架中；6出库待下架；7出库下架中； */
            /** taskStatus 为空则 taskStatusList 默认只查询 1,2,4,5,6,7个状态数据 */
            if (StringUtils.isEmpty(taskStatus)) {
                taskStatusList = Arrays.asList("1", "2","3", "4", "5", "6", "7");
            } else {
                taskStatusList = Arrays.asList(taskStatus.trim().split(","));
            }

            return ResponseResult.success().add("ShelfOnTask", this.baseMapper.queryShelfOnTasks(werks, whsLocationCode, taskType, taskStatusList, deliveryNoteSap, relateDocNumber, beginDate, endDate));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("Error when Query ShelfOn Tasks");
        }
    }

    @Override
    public ShelfOnTask getByRelateDocNumber(String relateDocNumber, String taskType) {
        if (StringUtils.isEmpty(relateDocNumber)){
            return null;
        }
        return shelfOnTaskMapper.getByRelateDocNumber(relateDocNumber, taskType);
    }

    @Override
    public List<ShelfOnTaskDtl> getDtlByUuid(String shelfOnTaskUuid) {
        if (StringUtils.isEmpty(shelfOnTaskUuid)){
            return null;
        }
        return shelfOnTaskDtlMapper.selectList(new QueryWrapper<ShelfOnTaskDtl>().eq("UUID", shelfOnTaskUuid));
    }

    @Override
    public List<ShelfOnTask> getTasksByRelatedDocId(String relateDocNumber, String taskType) {
        return this.baseMapper.selectList(new QueryWrapper<ShelfOnTask>().eq("RELATE_DOC_NUMBER",relateDocNumber).eq("TASK_TYPE",taskType));
    }
    @Override
    public List<ShelfOnTask> getTasksByRelatedDocId(String relateDocNumber) {
        return this.baseMapper.selectList(new QueryWrapper<ShelfOnTask>().eq("RELATE_DOC_NUMBER",relateDocNumber));
    }

    @Override
    public List<ShelfOnTask> getUnClosedTasks(String relateDocNumber) {
        return this.baseMapper.selectList(new QueryWrapper<ShelfOnTask>().eq("RELATE_DOC_NUMBER",relateDocNumber).ne("TASK_STATUS",'3'));
    }
}
